import cv2
import numpy as np
image = cv2.imread("ero.jpg")# 创建腐蚀和膨胀所需的卷积核
kernel = np.ones((5, 5), np.uint8)# 分离彩色图像的三个通道
b, g, r = cv2.split(image)# 对每个通道进行腐蚀操作
eroded_b = cv2.erode(b, kernel, iterations=1)
eroded_g = cv2.erode(g, kernel, iterations=1)
eroded_r = cv2.erode(r, kernel, iterations=1)# 对每个通道进行膨胀操作
dilated_b = cv2.dilate(b, kernel, iterations=1)
dilated_g = cv2.dilate(g, kernel, iterations=1)
dilated_r = cv2.dilate(r, kernel, iterations=1)# 合并腐蚀和膨胀的通道
eroded_image = cv2.merge((eroded_b, eroded_g, eroded_r))
dilated_image = cv2.merge((dilated_b, dilated_g, dilated_r))# 显示原始图像、腐蚀结果和膨胀结果
cv2.imshow('Original', image)
cv2.imshow('Eroded', eroded_image)
cv2.imshow('Dilated', dilated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()